package com.cxx.controller;

import com.aliyuncs.exceptions.ClientException;
import com.cxx.log.Log;
import com.cxx.mapper.BlogsMapper;
import com.cxx.mapper.UserMapper;
import com.cxx.pojo.*;
import com.cxx.service.BlogsService;
import com.cxx.utils.AliOssUtil;
import com.cxx.utils.JwtUtils;
import com.cxx.vo.CommentInfoVo;
import com.cxx.vo.ReplyInfoVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/person-web/api/*")
public class BlogsController {
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private BlogsService blogsService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogsMapper blogsMapper;
    @PostMapping ("/addBlogs")
    @Log
    public Result addBlogs(MultipartFile image, Blog blog,String token) throws IOException, ClientException {
        String imgUrl=aliOssUtil.upload(image);
        blog.setBlogImg(imgUrl);
        Claims claims=jwtUtils.parseJwt(token);
        String account= (String) claims.get("name");
        blog.setAccount(account);
        blogsService.addBlogs(blog);
        return Result.success("SUCCESS");
    }
    @GetMapping("/blogs")
    @Log
    public Result blogs(String token){
        String account= (String) jwtUtils.parseJwt(token).get("name");
        List<Blog> blogs= blogsService.blogs(account);
        return Result.sucess(blogs);
    }
    @GetMapping("/blogsByTag")
    @Log
    public Result blogsByTag(String token,String tag){
        String account= (String) jwtUtils.parseJwt(token).get("name");
        List<Blog> blogs= blogsService.blogsByTag(account,tag);
        return Result.sucess(blogs);
    }
    @GetMapping("/blogsList")
    @Log
    public Result blogsList(){
        List<Blog> blogs=blogsService.blogsList();
        for (Blog blog : blogs) {
            String account=blog.getAccount();
            if(userMapper.selectByAdminAccount(account)!=null){
                blog.setOwnerCall(userMapper.selectByAdminAccount(account).getAdminCall());
                blog.setOwnerImgUrl(userMapper.selectByAdminAccount(account).getAdminImgurl());
            }
            else {
                blog.setOwnerCall(userMapper.selectByUserAccount(account).getUserCall());
                blog.setOwnerImgUrl(userMapper.selectByUserAccount(account).getUserImgUrl());
            }
        }
        return Result.sucess(blogs);
    }
    @GetMapping("/blogById")
    @Log
    public Result blogById(Integer id){
        Blog blog =blogsService.blogById(id);
        return Result.sucess(blog);
    }
    @PostMapping("/writeComment")
    @Log
    public Result writeComment(String token,Comments comments){
        Claims claims = jwtUtils.parseJwt(token);
        String account= (String) claims.get("name");
        comments.setAccount(account);
        blogsService.writeComment(comments);
        return Result.success("ok");
    }
    @PostMapping("/writeReplay")
    @Log
    public Result writeReplay(String token,Replay replay){
        Claims claims=jwtUtils.parseJwt(token);
        String account= (String) claims.get("name");
        replay.setAccount(account);
        blogsService.writeReplay(replay);
        return Result.success("ok");
    }
    @GetMapping("/commentsByBlogId")
    @Log
    public Result getComments(Integer id){
        List<CommentInfoVo> commentInfoVoList=new ArrayList<>();
        List<Comments> comments=blogsService.getComments(id);
        //对comment遍历循环，并且赋值vo，如果有追评则增加
        for (Comments comment : comments) {
            List<ReplyInfoVo> replyInfoVoList = new ArrayList<>();
            if(userMapper.selectByAdminAccount(comment.getAccount())!=null){
                Admin adminComment=userMapper.selectByAdminAccount(comment.getAccount());
                CommentInfoVo commentInfoVo=new CommentInfoVo();
                commentInfoVo.setId(comment.getId());
                commentInfoVo.setAccount(comment.getAccount());
                commentInfoVo.setImgUrl(adminComment.getAdminImgurl());
                commentInfoVo.setCall(adminComment.getAdminCall());
                commentInfoVo.setContent(comment.getCommentsContent());
                commentInfoVo.setTime(comment.getCommentsTime());
                List<Replay> replayList=blogsMapper.getReply(comment.getId());
                for (Replay replay : replayList) {
                    if(userMapper.selectByAdminAccount(replay.getAccount())!=null){
                        Admin adminReply=userMapper.selectByAdminAccount(replay.getAccount());
                        String toAccountCall;
                        if(userMapper.selectByAdminAccount(replay.getToAccount())!=null){
                            toAccountCall=userMapper.selectByAdminAccount(replay.getToAccount()).getAdminCall();
                        }else {
                            toAccountCall=userMapper.selectByUserAccount(replay.getToAccount()).getUserCall();
                        }
                        ReplyInfoVo replyInfoVo=new ReplyInfoVo(replay.getAccount(),adminReply.getAdminImgurl(),adminReply.getAdminCall(),replay.getReplyTime(),replay.getReplyContent(),toAccountCall);
                        replyInfoVoList.add(replyInfoVo);
                    }
                    else {
                        User userReply=userMapper.selectByUserAccount(replay.getAccount());
                        String toAccountCall;
                        if(userMapper.selectByAdminAccount(replay.getToAccount())!=null){
                            toAccountCall=userMapper.selectByAdminAccount(replay.getToAccount()).getAdminCall();
                        }else {
                            toAccountCall=userMapper.selectByUserAccount(replay.getToAccount()).getUserCall();
                        }
                            ReplyInfoVo replyInfoVo=new ReplyInfoVo(replay.getAccount(),userReply.getUserImgUrl(),userReply.getUserCall(),replay.getReplyTime(),replay.getReplyContent(),toAccountCall);
                            replyInfoVoList.add(replyInfoVo);
                        }
                    }
                commentInfoVo.setReplyInfoVoList(replyInfoVoList);
                commentInfoVoList.add(commentInfoVo);
                }
            else {
                User userComment=userMapper.selectByUserAccount(comment.getAccount());
                CommentInfoVo commentInfoVo=new CommentInfoVo();
                commentInfoVo.setId(comment.getId());
                commentInfoVo.setAccount(comment.getAccount());
                commentInfoVo.setImgUrl(userComment.getUserImgUrl());
                commentInfoVo.setCall(userComment.getUserCall());
                commentInfoVo.setContent(comment.getCommentsContent());
                commentInfoVo.setTime(comment.getCommentsTime());
                List<Replay> replayList=blogsMapper.getReply(comment.getId());
                for (Replay replay : replayList) {
                    if(userMapper.selectByAdminAccount(replay.getAccount())!=null){
                        Admin adminReply=userMapper.selectByAdminAccount(replay.getAccount());
                        String toAccountCall;
                        if(userMapper.selectByAdminAccount(replay.getToAccount())!=null){
                            toAccountCall=userMapper.selectByAdminAccount(replay.getToAccount()).getAdminCall();
                        }else {
                            toAccountCall=userMapper.selectByUserAccount(replay.getToAccount()).getUserCall();
                        }
                        ReplyInfoVo replyInfoVo=new ReplyInfoVo(replay.getAccount(),adminReply.getAdminImgurl(),adminReply.getAdminCall(),replay.getReplyTime(),replay.getReplyContent(),toAccountCall);
                        replyInfoVoList.add(replyInfoVo);
                    }
                    else {
                        User userReply=userMapper.selectByUserAccount(replay.getAccount());
                        String toAccountCall;
                        if(userMapper.selectByAdminAccount(replay.getToAccount())!=null){
                            toAccountCall=userMapper.selectByAdminAccount(replay.getToAccount()).getAdminCall();
                        }else {
                            toAccountCall=userMapper.selectByUserAccount(replay.getToAccount()).getUserCall();
                        }
                        ReplyInfoVo replyInfoVo=new ReplyInfoVo(replay.getAccount(),userReply.getUserImgUrl(),userReply.getUserCall(),replay.getReplyTime(),replay.getReplyContent(),toAccountCall);
                        replyInfoVoList.add(replyInfoVo);
                    }
                }
                commentInfoVo.setReplyInfoVoList(replyInfoVoList);
                commentInfoVoList.add(commentInfoVo);
            }
        }
        return Result.sucess(commentInfoVoList);
    }
}
